package org.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.chen.dto.CommentUrlDTO;
import org.chen.enu.TaskStatus;
import org.chen.mapper.BatchListMapper;
import org.chen.mapper.CommentBatchMapper;
import org.chen.mapper.CommentTaskMapper;
import org.chen.model.CommentBatch;
import org.chen.model.CommentTask;
import org.chen.service.IBatchListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BatchListServiceImpl extends ServiceImpl<BatchListMapper, CommentBatch> implements IBatchListService {
    @Autowired
    private CommentBatchMapper batchMapper;

    @Autowired
    private CommentTaskMapper taskMapper;

    @Autowired
    private BatchStatsManager batchStatsManager;

    @Override
    @Transactional
    public void deleteBatchWithTasks(String batchNo) {
        // 删除关联的任务
        taskMapper.delete(new QueryWrapper<CommentTask>().eq("batch_no", batchNo));
        // 删除批次
        batchMapper.delete(new QueryWrapper<CommentBatch>().eq("batch_no", batchNo));

        batchStatsManager.deleteCountersForBatch(batchNo);
    }

    @Override
    public IPage<CommentBatch> getBatchList(Integer current, Integer pageSize) {
        Page<CommentBatch> page = new Page<>(current, pageSize);
        return batchMapper.selectPage(page,
                new QueryWrapper<CommentBatch>().orderByDesc("create_time"));
    }

    @Override
    @Transactional
    public void deleteBatchesWithTasks(List<String> batchNos) {
        // 删除关联的任务
        taskMapper.delete(new QueryWrapper<CommentTask>().in("batch_no", batchNos));
        // 删除批次
        batchMapper.delete(new QueryWrapper<CommentBatch>().in("batch_no", batchNos));

        batchStatsManager.deleteCountersForBatch(batchNos);
    }

    public List<CommentUrlDTO> getSuccessCommentUrls(List<String> batchNos) {
        return taskMapper.selectList(
                        new QueryWrapper<CommentTask>()
                                .select("batch_no", "target_url", "new_comment_url")
                                .in("batch_no", batchNos)
                                .isNotNull("new_comment_url")
                                .and(wrapper -> wrapper
                                        .eq("status", TaskStatus.SUCCESS.name())
                                        .or()
                                        .eq("status", TaskStatus.FAILED.name())
                                )
                ).stream()
                .map(task -> CommentUrlDTO.builder()
                        .batchNo(task.getBatchNo())
                        .targetUrl(task.getTargetUrl())
                        .newCommentUrl(task.getNewCommentUrl())
                        .build())
                .collect(Collectors.toList());
    }
}
